package com.tql.mall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.tql.mall.boss.client.BossFeignClient;
import com.tql.mall.common.constant.RedisConstant;
import com.tql.mall.item.service.ItemService;
import com.tql.mall.model.product.BaseAttrInfo;
import com.tql.mall.model.product.SkuInfo;
import com.tql.mall.product.client.ProductFeignClient;
import com.tql.mall.search.client.SearchFeignClient;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

/**
 * @Author: tyk
 * @Date: 2023/5/22 17:51
 * @Description:
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ProductFeignClient productFeignClient;


    @Autowired
    private BossFeignClient bossFeignClient;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private ThreadPoolExecutor executor;

    @Autowired
    private SearchFeignClient searchFeignClient;

@Override
public HashMap<String, Object> getItem(Long skuId) {
    HashMap<String, Object> res = new HashMap<>();

    // 通过布隆过滤器判断数据是否存在
    RBloomFilter<Object> bloom = redissonClient.getBloomFilter(
            RedisConstant.BLOOM_FILTER
    );
    if (!bloom.contains(skuId)) return res;

    // 获取sku信息（含图片列表）
    CompletableFuture<SkuInfo> skuInfoCf = CompletableFuture.supplyAsync(() -> {
        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
        res.put("skuInfo", skuInfo);
        return skuInfo;
    }, executor);
//        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
//        res.put("skuInfo", skuInfo);

    // 获取实时价格
    CompletableFuture<Void> priceCf = CompletableFuture.runAsync(
            () -> res.put("price", productFeignClient.getSkuPrice(skuId)),
            executor
    );
//        res.put("price", productFeignClient.getSkuPrice(skuId));

    // 获取平台信息
    CompletableFuture<Void> baseAttrCf = skuInfoCf.thenAcceptAsync(
            skuInfo -> {
                List<BaseAttrInfo> baseAttrInfoList = productFeignClient.getBaseAttrInfoList(skuId);
                List<HashMap<String, String>> attrList = baseAttrInfoList.stream().map(e -> {
                    HashMap<String, String> map = new HashMap<>();
                    map.put("attrName", e.getAttrName());
                    map.put("attrValue", e.getAttrValueList().get(0).getValueName());
                    return map;
                }).collect(Collectors.toList());
                res.put("skuAttrList", attrList);
            },
            executor
    );
//        List<BaseAttrInfo> baseAttrInfoList = productFeignClient.getBaseAttrInfoList(skuId);
//        List<HashMap<String, String>> attrList = baseAttrInfoList.stream().map(e -> {
//            HashMap<String, String> map = new HashMap<>();
//            map.put("attrName", e.getAttrName());
//            map.put("attrValue", e.getAttrValueList().get(0).getValueName());
//            return map;
//        }).collect(Collectors.toList());
//        res.put("skuAttrList", attrList);

    // 获取三级分类
    CompletableFuture<Void> categoryCf = skuInfoCf.thenAcceptAsync(skuInfo -> {
        res.put(
                "categoryView",
                bossFeignClient.getCategoryView(skuInfo.getCategory3Id())
        );
    }, executor);
//        res.put(
//                "categoryView",
//                bossFeignClient.getCategoryView(skuInfo.getCategory3Id())
//        );

    // 获取销售属性和选中状态
    CompletableFuture<Void> saleAttrCf = skuInfoCf.thenAcceptAsync(
            skuInfo -> res.put(
                    "spuSaleAttrList",
                    productFeignClient.getSpuSaleAttrList(skuInfo.getId(), skuInfo.getSpuId())
            ), executor);
//        res.put(
//                "spuSaleAttrList",
//                productFeignClient.getSpuSaleAttrList(skuInfo.getId(), skuInfo.getSpuId())
//        );

    // 获取商品切换数据
    CompletableFuture<Void> idsMapCf = skuInfoCf.thenAcceptAsync(
            skuInfo -> res.put(
                    "valuesSkuJson",
                    JSON.toJSONString(
                            productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId())
                    )
            ), executor);
//        res.put(
//                "valuesSkuJson",
//                JSON.toJSONString(
//                        productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId())
//                )
//        );

    // 获取海报信息
    CompletableFuture<Void> posterCf = skuInfoCf.thenAcceptAsync(
            skuInfo -> res.put(
                    "spuPoserList",
                    productFeignClient.getSpuPosterList(skuInfo.getSpuId())
            ), executor);
//        res.put(
//                "spuPoserList",
//                productFeignClient.getSpuPosterList(skuInfo.getSpuId())
//        );


    // 更新商品热度
    CompletableFuture<Void> scoreCf = CompletableFuture.runAsync(
            () -> searchFeignClient.incrHotScore(skuId),
            executor
    );
//    searchFeignClient.incrHotScore(skuId);

    // 任务组合
    CompletableFuture.allOf(
            skuInfoCf, priceCf, baseAttrCf, categoryCf,
            saleAttrCf, idsMapCf, posterCf, scoreCf
    ).join();

    return res;
}
}
